<?php

namespace app\common\model;

use think\facade\Db;
use think\Model;

class Exercises extends Model
{
    //自动生成写入时间
    protected $autoWriteTimestamp = true;

    public function exercises($data)
    {

        if (empty($data['subject_id'])) {
            return Db::table('exercises')
                ->alias('e')
                ->join('subject s', 'e.subject_id = s.id')
                ->field('e.*,s.name as subject_name')
                ->paginate($data);
        } else {
            return Db::table('exercises')
                ->alias('e')
                ->join('subject s', 'e.subject_id = s.id')
                ->field('e.*,s.name as subject_name')
                ->where(['e.subject_id' => $data['subject_id']])
                ->paginate($data);
        }
    }
}